package ua.openmedis.web.umdns;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ua.openmedis.service.api.UmdnsService;
import ua.openmedis.service.impl.UmdnsServiceImpl;

@SessionScoped
@ManagedBean(name = "umdnsController")
public class UmdnsController {

	private final static Logger log = LoggerFactory
			.getLogger(UmdnsController.class);

	UmdnsTable umdnsTable = new UmdnsTable();

	UmdnsService umdnsService = new UmdnsServiceImpl();
	UmdnsViewBean viewBean = new UmdnsViewBean();

	public UmdnsController() {
		log.debug("UmdnsController constructor. Searching UMDNS codes...");
		umdnsTable.setUmdnsList(umdnsService.findAllUmdnsCodes());
		log.debug("UMDNS codes were found");
	}

	public void findUmdns() {
		String toFind = viewBean.getSearchString();
		log.debug("Searching UMDNS: '" + toFind + "'");
		if(toFind == null || toFind.length() == 0){
			umdnsTable.setUmdnsList(umdnsService.findAllUmdnsCodes());
		} else {
			umdnsTable.setUmdnsList(umdnsService.findCodesByName(toFind));
		}
		log.debug("Searching UMDNS: '" + toFind + "' finished");
	}

	// ------------------ GETTERS SETTERS -----------------------------------
	public UmdnsTable getUmdnsTable() {
		return umdnsTable;
	}

	public void setUmdnsTable(UmdnsTable umdnsTable) {
		this.umdnsTable = umdnsTable;
	}

	public UmdnsViewBean getViewBean() {
		return viewBean;
	}

	public void setViewBean(UmdnsViewBean viewBean) {
		this.viewBean = viewBean;
	}
}
